<?php



require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_FLIER_PRICE.php' );
require_once( '../system/mdao/Class_M_COST_FLIER_PRICE_AREA.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_QR,$login['staff_auth']);

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['COST_FLIER_PRICE'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_FLIER_PRICE'] = $forms;
}

if (empty( $forms['sort'] ) ) {
	$forms['sort'] = 1;

}

$dosearch = FALSE ;

if ($forms['sort'] == 1) {
	$dao = new Class_M_COST_FLIER_PRICE ;
	
} else {
	$dao = new Class_M_COST_FLIER_PRICE_AREA ;
	
}

$wherearr =  array();
if ( isset( $forms['block_cd'] ) &&  ( !empty( $forms['block_cd'] ) ) ){
	$wherearr[] = sprintf(" c.block_cd =  '%s' ",  $forms['block_cd'] );
}
/*
if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" c.shop_cd =  '%s' ",  $forms['shop_cd'] );
}

if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'd' );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, 'd' );
}
*/

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	if ($forms['sort'] == 1){
		$tmpfilename = "tirasi_price".date("Ymd_His");
	}else {
		$tmpfilename = "area_price".date("Ymd_His");
	}
	header("Content-Type: application/octet-stream");
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data      = $dao->getsearch( $wherearr,   $forms['year4'] );
	$colums = array();
	//列名
	$colums = array();

//1 string code 2 integer 3 date 4 string ""必要 
if ($forms['sort'] == 1){
	$col_heads = array(
		'year4'        => array( '年度',        1),
		'sort'         => array( '種類',        1),
		'shop_cd'      => array( '店舗コード',  1),
		'shop_name'    => array( '店舗名',      1),
		'b2'           => array( 'B2価格',      1),
		'b3'           => array( 'B3価格',      1),
		'b4'           => array( 'B4価格',      1),
	);
	$sortcap = '通常';
} else {
	$col_heads = array(
		'year4'        => array( '年度',        1),
		'sort'         => array( '種類',        1),
		'shop_cd'      => array( 'パターンコード',  1),
		'shop_name'    => array( 'パターン名',      1),
		'b2'           => array( 'B2価格',      1),
		'b3'           => array( 'B3価格',      1),
		'b4'           => array( 'B4価格',      1),
	);
	$sortcap = '地域';
}	
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	foreach ($data as $k => $v) {
		$data[$k]["year4"] =  $forms['year4'] ;
		$data[$k]["sort"]  = $sortcap;
	}
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

/*
if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
	$st = ( $page['current'] -1 ) * PAGE_MAX;
}else{
	$page['current'] = 1;
	$st = 0;
}

$page['item'] = PAGE_MAX;
*/

if ($dosearch) {
	$page = NULL;
	//$page内容は関数内部更新されます。
	$data      = $dao->getsearch( $wherearr,   $forms['year4'] );

	foreach ($data as $k=>$v) {
		if (empty($v["b2"])) {
			$data[$k]["b2"] = "";
		}
		if (empty($v["b3"])) {
			$data[$k]["b3"] = "";
		}
		if (empty($v["b4"])) {
			$data[$k]["b4"] = "";
		}
	}
}

$page["cnt"] = count($data);

$smarty->assign('sorts', $aoki_flier_price_sort);
if (empty($forms["year4"])) {
	$forms["year4"] = date("Y");
}
if( isset($forms) )     $smarty->assign('forms', $forms);
if( isset($data) )      $smarty->assign('data', $data);

if( isset($patterns) )  $smarty->assign('pattern', $patterns);
if( isset($page) )      $smarty->assign('page', $page);


$smarty->assign('year4',  Class_PWD::get_years());
$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );

$smarty->assign('menu',   $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
//$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_flier_price.html');

?>